package com.zb.spring5.properties;

import com.zb.spring5.inteface.MessagePrivider;
import com.zb.spring5.inteface.MessageRender;

import java.util.Properties;

public class MessageSupportFactory {

    private static MessageSupportFactory instance;

    private Properties props;
    private MessageRender render;
    private MessagePrivider privider;

    /**
     * 加载属性文件,演示spring容器注入特性
     */
    public MessageSupportFactory() {
        props = new Properties();
        try {
            props.load(this.getClass().getResourceAsStream("/msf.properties"));
            String renderClass = props.getProperty("render.class");
            String prividerClass = props.getProperty("privider.class");
            render = (MessageRender)Class.forName(renderClass).newInstance();
            privider = (MessagePrivider)Class.forName(prividerClass).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    static {
        /**
         * 演示一启动就加载配置文件
         */
        instance = new MessageSupportFactory();
    }

    public static MessageSupportFactory getInstance() {
        return instance;
    }

    public MessageRender getRender() {
        return render;
    }

    public MessagePrivider getPrivider() {
        return privider;
    }
}
